package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.commentlib.app.BaseFragmentActivity;
import com.example.myapplication.databinding.ActivityMvvmBinding;
import com.example.myapplication.fragment.FindWorkFragment;
import com.example.myapplication.fragment.RecordFragment;

public class MvvmActivity extends BaseFragmentActivity<ActivityMvvmBinding> {

    private ActivityMvvmBinding mvvmBinding;

    private FindWorkFragment findWorkFragment;
    private RecordFragment recordFragment;

    @Override
    public int layoutID() {
        return R.layout.activity_mvvm;
    }

    @Override
    public void initView(ActivityMvvmBinding binding) {
        mvvmBinding = binding;
        mvvmBinding.setClick(this);
        seletor(0);
    }

    public void workClick(View view){
        seletor(0);
    }

    public void recordClick(View view){
        seletor(1);
    }

    private void seletor(int i) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        resetIcon();
        hideFragment(transaction);
        switch (i){
            case 0:
                if (findWorkFragment == null){
                    findWorkFragment = new FindWorkFragment();
                    transaction.add(R.id.fl_frame, findWorkFragment);
                }else {
                    transaction.show(findWorkFragment);
                }
                mvvmBinding.ivWord.setBackgroundResource(R.mipmap.work_icon_2);
                mvvmBinding.tvWork.setTextColor(getResources().getColor(R.color.heise_3));
                break;
            case 1:
                if (recordFragment == null){
                    recordFragment = new RecordFragment();
                    transaction.add(R.id.fl_frame, recordFragment);
                }else {
                    transaction.show(recordFragment);
                }
                mvvmBinding.ivRecord.setBackgroundResource(R.mipmap.work_icon_2);
                mvvmBinding.tvRecord.setTextColor(getResources().getColor(R.color.heise_9));
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        if (findWorkFragment != null){
            transaction.hide(findWorkFragment);
        }
        if (recordFragment != null){
            transaction.hide(recordFragment);
        }
    }

    private void resetIcon(){
        mvvmBinding.ivWord.setBackgroundResource(R.mipmap.work_icon_1);
        mvvmBinding.ivRecord.setBackgroundResource(R.mipmap.work_icon_1);
        mvvmBinding.tvWork.setTextColor(getResources().getColor(R.color.heise_9));
        mvvmBinding.tvRecord.setTextColor(getResources().getColor(R.color.heise_9));
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }

}